<?php
class Application_Model_User
{
	protected $_dbTable;
	public $_mId;
	public $_mUsername;
	public $_mEmail;
	public $_mPassword;
	public $_mRole;
	public $_mStatus;

	public function getId(){
		return $this->_mId;
	}
	public function setId($value){
		$this->_mId = $value;
		return $this;
	}
	public function getUsername(){
		return $this->_mUsername;
	}
	public function setUsername($value){
		$this->_mUsername = $value;
		return $this;
	}
	public function getEmail(){
		return $this->_mEmail;
	}
	public function setEmail($value){
		$this->_mEmail = $value;
		return $this;
	}
	public function getPassword(){
		return $this->_mPassword;
	}
	public function setPassword($value){
		$this->_mPassword = $value;
		return $this;
	}
	public function getRole(){
		return $this->_mRole;
	}
	public function setRole($value){
		$this->_mRole = $value;
		return $this;
	}
	public function getStatus(){
		return $this->_mStatus;
	}
	public function setStatus($value){
		$this->_mStatus = $value;
		return $this;
	}


	public function setDbTable($dbTable){
		if (is_string($dbTable))
			$dbTable = new $dbTable();
		if (!$dbTable instanceof Zend_Db_Table_Abstract)
			throw new Exception('Invalid table data gateway provided');
		$this->_dbTable = $dbTable;
		return $this;
	}
	public function getDbTable(){
		if (null === $this->_dbTable)
			$this->setDbTable('Application_Model_DbTable_User');
		return $this->_dbTable;
	}
	public function save(Application_Model_User $old = null){
		$item = $this;
		$data = array();
		if(isset($item->_mId)) $data['id'] = $item->_mId;
		if(isset($item->_mUsername)) $data['username'] = $item->_mUsername;
		if(isset($item->_mEmail)) $data['email'] = $item->_mEmail;
		if(isset($item->_mPassword)) $data['password'] = $item->_mPassword;
		if(isset($item->_mRole)) $data['role'] = $item->_mRole;
		if(isset($item->_mStatus)) $data['status'] = $item->_mStatus;

		if($old === null)
			$this->getDbTable()->insert($data);
		else{
			$key = array();
			$old->_mId === null ? $key[] = 'id is NULL' : $key['id = ?'] =  $old->_mId;

			$this->getDbTable()->update($data, $key);
		}
	}
	public function delete(){
		$item = $this;
		$old = $item;
		$key = array();
			$old->_mId === null ? $key[] = 'id is NULL' : $key['id = ?'] =  $old->_mId;

		$this->getDbTable()->delete($key);
	}
	public function fetchAll($where = null, $order = null, $count = null, $offset = null){
		$resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
		$entries   = array();
		foreach ($resultSet as $row) {
			$item = new Application_Model_User();
			if(isset($row->id))$item->_mId = $row->id;
			if(isset($row->username))$item->_mUsername = $row->username;
			if(isset($row->email))$item->_mEmail = $row->email;
			if(isset($row->password))$item->_mPassword = $row->password;
			if(isset($row->role))$item->_mRole = $row->role;
			if(isset($row->status))$item->_mStatus = $row->status;

			$entries[] = $item;
		}
		return $entries;
	}
}
